<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeGridProps } from '#/adapter/vxe-table';
import type { DictType } from '#/api/system/dict/dict-type';

import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';

import { Modal, Popconfirm, Space } from 'ant-design-vue';

import { useVbenVxeGrid, vxeCheckboxChecked } from '#/adapter/vxe-table';
import { dictTypeList, dictTypeRemove } from '#/api/system/dict/dict-type';

import { emitter } from '../mitt';
import { columns, querySchema } from './data';
import dictTypeModal from './dict-type-modal.vue';

const formOptions: VbenFormProps = {
  schema: querySchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2',
};

const gridOptions: VxeGridProps = {
  columns: columns(),
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await dictTypeList({
          current: page.currentPage,
          size: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  id: 'system-dict-type-index',
};

const lastDictType = ref('');

const [BasicTable, tableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
  gridEvents: {
    cellClick: ({ row: { dictType } }) => {
      if (lastDictType.value === dictType) {
        return;
      }
      emitter.emit('rowClick', dictType);
      lastDictType.value = dictType;
    },
  },
});
const [DictTypeModal, modalApi] = useVbenModal({
  connectedComponent: dictTypeModal,
});

function handleAdd() {
  modalApi.setData({});
  modalApi.open();
}

async function handleEdit(record: DictType) {
  modalApi.setData({ id: record.id });
  modalApi.open();
}

async function handleDelete(row: DictType) {
  await dictTypeRemove([row.id]);
  await tableApi.query();
  // dictType行删除后，右侧的dictData也需要重新渲染
  emitter.emit('rowClick', '');
}

function handleMultiDelete() {
  const rows = tableApi.grid.getCheckboxRecords();
  const ids = rows.map((row: DictType) => row.id);
  Modal.confirm({
    title: $t('common.tip'),
    okType: 'danger',
    content: $t('ui.actionMessage.deleteConfirm', [ids.length]),
    onOk: async () => {
      await dictTypeRemove(ids);
      await tableApi.query();
      // dictType行删除后，右侧的dictData也需要重新渲染
      emitter.emit('rowClick', '');
    },
  });
}
</script>

<template>
  <div>
    <BasicTable id="dict-type" :table-title="$t('system.dictType.tableTitle')">
      <template #toolbar-tools>
        <Space>
          <a-button
            :disabled="!vxeCheckboxChecked(tableApi)"
            danger
            type="primary"
            @click="handleMultiDelete"
          >
            {{ $t('common.delete') }}
          </a-button>
          <a-button type="primary" @click="handleAdd">
            {{ $t('common.create') }}
          </a-button>
        </Space>
      </template>
      <template #action="{ row }">
        <Space>
          <GhostButton @click.stop="handleEdit(row)">
            {{ $t('common.edit') }}
          </GhostButton>
          <Popconfirm
            placement="left"
            :title="$t('common.deleteConfirm')"
            @confirm="handleDelete(row)"
          >
            <GhostButton danger @click.stop="">
              {{ $t('common.delete') }}
            </GhostButton>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <DictTypeModal @reload="tableApi.query()" />
  </div>
</template>
